package com.rdc.example.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

public class EmialUtil {


    public static void main(String[] args) throws Exception {
        String toEmail = "a2006868@qq.com";
        String subject="重置密码";
        String content="1we1f1v5v@@3344";
        System.out.println(sendEmail(toEmail,subject,content));
    }



    public static boolean sendEmail(String toEmail, String subject, String content) throws Exception {
        // 发件人邮箱
        final String fromEmail = "a2006868@qq.com";
        // 邮箱密码或授权码（视邮箱服务商要求）tkdhrnegifmbigei
        final String password = "tkdhrnegifmbigei";
        // 收件人邮箱
        //final String toEmail = "1297625861@qq.com";

        // 配置 SMTP 服务器属性
        Properties properties = new Properties();
        // SMTP 主机名（如 Gmail: smtp.gmail.com）
        properties.put("mail.smtp.host", "smtp.qq.com");
        // SMTP 端口号
        properties.put("smtp.qq.com", "587");
        // 是否需要认证
        properties.put("mail.smtp.auth", "true");
        // 启用 STARTTLS
        properties.put("mail.smtp.starttls.enable", "true");

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        });

        try {
            // 创建邮件内容
            Message message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(fromEmail));
            // 设置收件人
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            // 设置邮件主题
            message.setSubject(subject);
            // 设置邮件正文
            message.setText(content);

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

  /*public static void main(String[] args) {
        // 发件人邮箱
        final String fromEmail = "a2006868@qq.com";
        // 邮箱密码或授权码（视邮箱服务商要求）tkdhrnegifmbigei
        final String password = "tkdhrnegifmbigei";
        // 收件人邮箱
        final String toEmail = "1297625861@qq.com";

        // 配置 SMTP 服务器属性
        Properties properties = new Properties();
        // SMTP 主机名（如 Gmail: smtp.gmail.com）
        properties.put("mail.smtp.host", "smtp.qq.com");
        // SMTP 端口号
        properties.put("smtp.qq.com", "587");
        // 是否需要认证
        properties.put("mail.smtp.auth", "true");
        // 启用 STARTTLS
        properties.put("mail.smtp.starttls.enable", "true");

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        });

        try {
            // 创建邮件内容
            Message message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(fromEmail));
            // 设置收件人
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            // 设置邮件主题
            message.setSubject("测试邮件");
            // 设置邮件正文
            message.setText("这是通过 Java 发送的测试邮件。");

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }*/
}
